home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8564 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  48 lines

  1. Path: ip-salem3-09.teleport.com!user
  2. From: dynamix@teleport.com (Steve Budrys)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Run time dynamic 2-D array?
  5. Date: Wed, 14 Feb 1996 17:13:52 -0800
  6. Organization: Dynamix Trading
  7. Message-ID: <dynamix-1402961713520001@ip-salem3-09.teleport.com>
  8. References: <4flcq9$i1k@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: ip-salem3-09.teleport.com
  10.  
  11. In article <4flcq9$i1k@vixen.cso.uiuc.edu>, WEIMIN YANG <w-yang3> wrote:
  12.  
  13. > I need to use a 2-D array. I use following code.
  14. > void tryit(int a, int b) {
  15. > float c= new float[b][a];
  16. > }
  17. > I get compile error. Is there another way to do it? By the way, I don't
  18. want to
  19. > use 1-D array to implement it.
  20.  
  21. try
  22.  
  23. float **MakeMatrix(int nRows, int nCols)
  24. {
  25.    float **matrix = new float *[nRows];
  26.    while(nRows--)
  27.       matrix[nRows] = new float[nCols];
  28.    return matrix;
  29. }
  30.  
  31. (note that this gives you the effect of new float [a][b], not [b][a] )
  32.  
  33. You'll also need to be able to tear it down:
  34.  
  35. void DestroyMatrix(float **matrix, int nRows)
  36. {
  37.    while(nRows--)
  38.       delete [] matrix[nRows];
  39.    delete [] matrix;
  40. }
  41.  
  42. -- 
  43. Steve Budrys
  44. Dynamix Trading
  45.